package org.example.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import org.example.model.PageInfo;
import org.example.model.Sysrole;
import org.example.service.ResourcesService;
import org.example.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


//定义表现层控制器bean
@Controller
@ResponseBody
@RequestMapping("/save")
public class UserController {

    @Autowired
    private ResourcesService resourcesService;

    @GetMapping
    public Result save(PageInfo pageInfo) {
        PageInfo pf = new PageInfo();
        pf.setPageNo((pageInfo.getPageNo()* pageInfo.getPageSize()) - pageInfo.getPageSize());
        pf.setPageSize(pageInfo.getPageSize());
        List<Sysrole> list = resourcesService.save(pf);
        Map map = new HashMap();
        map.put("list",list);
        map.put("total",resourcesService.all().size());
        return Result.success("查询成功", 200, map);
    }

    @RequestMapping("/getCode")
    public void getCode(HttpServletResponse resp, HttpSession session) throws IOException {
        CircleCaptcha captcha = CaptchaUtil.createCircleCaptcha(116, 36, 4, 5);
        //得到code
        String code = captcha.getCode();
        System.out.println(code);
        //放到session
        session.setAttribute("code", code);
        ServletOutputStream outputStream = resp.getOutputStream();
        captcha.write(outputStream);
        outputStream.close();
    }

}
